package com.muyu.cart.controller;

import com.muyu.cart.domain.CartManager;
import com.muyu.cart.domain.req.*;
import com.muyu.cart.domain.resp.CartListResp;
import com.muyu.cart.domain.resp.CartStatusCheckResp;
import com.muyu.cart.service.CartService;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.security.annotation.RequiresPermissions;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 购物车控制器
 *
 * @author JiaCheng
 * @version 1.0
 * @since 2025-09-21
 */
@RestController
@RequestMapping("/cart")
@Tag(name = "CartController", description = "购物车管理")
public class CartController extends BaseController {

    @Autowired
    private CartService cartService;

    /**
     * 查询购物车列表 全查
     */
    @Operation(summary = "查询购物车列表", description = "查询购物车列表")
    @RequiresPermissions("cart:list")
    @PostMapping("/list")
    public Result<List<CartListResp>> pageQuery(@RequestBody @Validated CartListReq req) {
        List<CartListResp> result = cartService.selectCartListPage(req);
        return Result.success(result);
    }

    /**
     * 添加商品到购物车
     */
    @Operation(summary = "添加商品到购物车", description = "添加商品到购物车")
    @RequiresPermissions("cart:add")
    @PostMapping("/add")
    public Result<Void> addToCart(@RequestBody @Validated CartAddReq req) {
        cartService.addToCart(req);
        return Result.success();
    }

    /**
     * 更新商品数量
     */
    @Operation(summary = "更新商品数量", description = "更新购物车商品数量")
    @RequiresPermissions("cart:edit")
    @PostMapping("/updateQuantity")
    public Result<Void> updateQuantity(@RequestBody @Validated CartUpdateQuantityReq req) {
        cartService.updateQuantity(req);
        return Result.success();
    }

    /**
     * 勾选/取消购物车商品
     */
    @Operation(summary = "勾选/取消购物车商品", description = "批量勾选或取消勾选购物车商品")
    @RequiresPermissions("cart:edit")
    @PostMapping("/toggleSelect")
    public Result<Void> toggleSelect(@RequestBody @Validated CartToggleSelectReq req) {
        cartService.toggleSelect(req);
        return Result.success();
    }

    /**
     * 全选/全不选
     */
    @Operation(summary = "全选/全不选", description = "全选或全不选购物车商品")
    @RequiresPermissions("cart:edit")
    @PostMapping("/selectAll")
    public Result<Void> selectAll(@RequestBody @Validated CartSelectAllReq req) {
        cartService.selectAll(req);
        return Result.success();
    }

    /**
     * 删除购物车商品
     */
    @Operation(summary = "删除购物车商品", description = "批量删除购物车商品")
    @RequiresPermissions("cart:delete")
    @PostMapping("/delete")
    public Result<Void> deleteCartItems(@RequestBody List<Long> cartIds) {
        cartService.deleteCartItems(cartIds);
        return Result.success();
    }

    /**
     * 清空购物车
     */
    @Operation(summary = "清空购物车", description = "清空当前用户的所有购物车商品")
    @RequiresPermissions("cart:delete")
    @PostMapping("/clear")
    public Result<Void> clearCart() {
        cartService.clearCart();
        return Result.success();
    }

    /**
     * 获取购物车商品总数
     */
    @Operation(summary = "获取购物车商品总数", description = "获取当前用户购物车商品总数")
    @RequiresPermissions("cart:query")
    @GetMapping("/totalCount")
    public Result<Integer> getTotalCount() {
        int count = cartService.getTotalCount();
        return Result.success(count);
    }

    /**
     * 获取选中商品数量
     */
    @Operation(summary = "获取选中商品数量", description = "获取当前用户购物车选中商品数量")
    @RequiresPermissions("cart:query")
    @GetMapping("/selectedCount")
    public Result<Integer> getSelectedCount() {
        int count = cartService.getSelectedCount();
        return Result.success(count);
    }

    /**
     * 获取选中商品总金额
     */
    @Operation(summary = "获取选中商品总金额", description = "获取当前用户购物车选中商品总金额")
    @RequiresPermissions("cart:query")
    @GetMapping("/selectedAmount")
    public Result<BigDecimal> getSelectedAmount() {
        BigDecimal amount = cartService.getSelectedAmount();
        return Result.success(amount);
    }

    /**
     * 获取选中商品列表
     */
    @Operation(summary = "获取选中商品列表", description = "获取当前用户购物车选中商品列表")
    @RequiresPermissions("cart:query")
    @GetMapping("/selectedItems")
    public Result<List<CartListResp>> getSelectedItems() {
        List<CartListResp> items = cartService.getSelectedItems();
        return Result.success(items);
    }

    /**
     * 检查购物车商品状态
     */
    @Operation(summary = "检查购物车商品状态", description = "检查购物车商品状态，包括库存、价格等")
    @RequiresPermissions("cart:query")
    @GetMapping("/checkStatus")
    public Result<List<CartStatusCheckResp>> checkCartStatus() {
        List<CartStatusCheckResp> statusList = cartService.checkCartStatus();
        return Result.success(statusList);
    }

    /**
     * 合并购物车
     */
    @Operation(summary = "合并购物车", description = "合并本地购物车到服务器购物车")
    @RequiresPermissions("cart:add")
    @PostMapping("/merge")
    public Result<Void> mergeCart(@RequestBody List<CartManager> cartItems) {
        cartService.mergeCart(cartItems);
        return Result.success();
    }


}